/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.clientpackets;

import java.util.Collection;

import jmmos.gameserver.TaskPriority;
import jmmos.gameserver.model.MMOObject;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.network.serverpackets.ExBrExtraUserInfo;
import jmmos.gameserver.network.serverpackets.SpawnItem;
import jmmos.gameserver.network.serverpackets.UserInfo;

public class RequestRecordInfo extends MMOGameClientPacket
{
	private static final String _C__6E_REQUEST_RECORD_INFO = "[C] 6E RequestRecordInfo";
	
	/**
	 * urgent messages, execute immediately
	 * @return
	 */
	public TaskPriority getPriority()
	{
		return TaskPriority.PR_NORMAL;
	}
	
	@Override
	protected void readImpl()
	{
		// trigger
	}
	
	@Override
	protected void runImpl()
	{
		MMOPcInstance _activeChar = getClient().getActiveChar();
		
		if (_activeChar == null)
		{
			return;
		}
		
		_activeChar.sendPacket(new UserInfo(_activeChar));
		_activeChar.sendPacket(new ExBrExtraUserInfo(_activeChar));
		
		Collection<MMOObject> objs = _activeChar.getKnownList().getKnownObjects().values();
		for (MMOObject object : objs)
		{
			if (object.getPoly().isMorphed() && object.getPoly().getPolyType().equals("item"))
			{
				_activeChar.sendPacket(new SpawnItem(object));
			}
			else
			{
				object.sendInfo(_activeChar);
				
				if (object instanceof MMOCharacter)
				{
					// Update the state of the MMOCharacter object client
					// side by sending Server->Client packet
					// MoveToPawn/CharMoveToLocation and AutoAttackStart to
					// the MMOPcInstance
					MMOCharacter obj = (MMOCharacter) object;
					if (obj.getAI() != null)
					{
						obj.getAI().describeStateToPlayer(_activeChar);
					}
				}
			}
		}
	}
	
	@Override
	public String getType()
	{
		return _C__6E_REQUEST_RECORD_INFO;
	}
}
